﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Text.RegularExpressions;

namespace Eugenhome.Atrium.LibRu.Web
{
    /// <summary>
    /// 
    /// </summary>
    public class LibRuBookItem
    {
        /// <summary></summary>
        public bool Valid { get; private set; }

        /// <summary></summary>
        public string Content { get; private set; }

        /// <summary></summary>
        public string Mode = "";

        /// <summary>
        /// 
        /// </summary>
        /// <param name="url"></param>
        public LibRuBookItem(string rawContent)
        {
            Valid = !rawContent.Equals(string.Empty);

            if (Valid)
            {
                if (!rawContent.IndexOf("<dd>", StringComparison.OrdinalIgnoreCase).Equals(-1))
                {
                    Mode = "dd";

                    int index = rawContent.IndexOf("<dd>", StringComparison.OrdinalIgnoreCase);
                    int index2 = rawContent.LastIndexOf("<dd>", StringComparison.OrdinalIgnoreCase);
                    Content = rawContent.Substring(index, index2 - index);
                    Content = Regex.Replace(Content, "<dd>", string.Empty, RegexOptions.IgnoreCase);
                    Content = Regex.Replace(Content, "&nbsp;", string.Empty, RegexOptions.IgnoreCase);
                }
                else
                    if (rawContent.StartsWith("<html>", StringComparison.OrdinalIgnoreCase))
                    {
                        Mode = "html";

                        Content = rawContent;

                        //int index = Content.IndexOf("<body>", StringComparison.OrdinalIgnoreCase);
                        //int index2 = Content.IndexOf("</body>", StringComparison.OrdinalIgnoreCase);
                        //Content = Content.Substring(index, index2 - index + 7);

                        ////Content = PageContent.StripTagsRegex(rawContent.Substring(index, index2 - index + 7));
                        Content = Content.Replace("\n ", "$$$");
                        Content = Content.Replace("\n\n", "&&&");
                        Content = Content.Replace("\n", "");
                        Content = Content.Replace("$$$", "\n ");
                        Content = Content.Replace("&&&", "\n\n");
                        Content = "<html>" + Content + "</html>";
                    }
            }
        }
    }
}